/**
 * 
 */
package spl.army.ut;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import spl.army.ItemRequired;
import spl.army.Mission;
import spl.army.MissionHolder;
import spl.army.Sergeant;

/**
 * @author Ruby
 *
 */
public class MissionHolderTest {
	private MissionHolder missionHolder;
	private Mission fiveHoursMission;
	private Mission twoHoursMission;	
	private Mission noItemsReqMission;
	private Mission threeItemsReqMission;
	private Sergeant dummySergeant;
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {		
		this.dummySergeant = new Sergeant(
							"Moshe",
							"2",
							"sniper",
							"2",
							"shortestMission",
							"2");		
		
		this.missionHolder = new MissionHolder();
		ArrayList<ItemRequired> noItemsRequireds = new ArrayList<ItemRequired>();
		ArrayList<Mission> noPreReq = new ArrayList<Mission>();		
		ArrayList<ItemRequired> oneItemReq = new ArrayList<ItemRequired>();
		oneItemReq.add(new ItemRequired("axe", 1));
		
		ArrayList<ItemRequired> threeItemReq = new ArrayList<ItemRequired>();
		threeItemReq.add(new ItemRequired("sword", 1));
		threeItemReq.add(new ItemRequired("laser", 1));
		threeItemReq.add(new ItemRequired("magic", 1));
		
		ArrayList<ItemRequired> twoItemReq = new ArrayList<ItemRequired>();
		twoItemReq.add(new ItemRequired("sword", 1));
		twoItemReq.add(new ItemRequired("laser", 1));	
		
		this.fiveHoursMission = new Mission(
							"longest_mission",
							"sniper",
							5,
							oneItemReq,
							noPreReq);
		missionHolder.addMission(this.fiveHoursMission);
		this.noItemsReqMission = new Mission(
				"no_items",
				"sniper",
				4,
				noItemsRequireds,
				noPreReq);
		missionHolder.addMission(this.noItemsReqMission);
		this.threeItemsReqMission = new Mission(
				"many_items",
				"sniper",
				3,
				threeItemReq,
				noPreReq);
		missionHolder.addMission(this.threeItemsReqMission);		
		this.twoHoursMission = new Mission(
				"shortest_mission",
				"sniper",
				2,
				twoItemReq,
				noPreReq);
		missionHolder.addMission(this.twoHoursMission);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#MissionHolder()}.
	 */
	@Test
	public void testMissionHolder() {
		MissionHolder m = new MissionHolder();
		assertTrue(m.count()==0);
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#getMissionList()}.
	 */
	@Test
	public void testGetMissionList() {
		List<Mission> ml = this.missionHolder.getMissionList();
		assertEquals(ml.size(), 4);
		assertTrue(ml.indexOf(this.fiveHoursMission) != -1);
		assertTrue(ml.indexOf(this.noItemsReqMission) != -1);
		assertTrue(ml.indexOf(this.threeItemsReqMission) != -1);
		assertTrue(ml.indexOf(this.twoHoursMission) != -1);
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#addMission(spl.army.Mission)}.
	 */
	@Test
	public void testAddMission() {
		MissionHolder m = new MissionHolder();
		List<Mission> ml = m.getMissionList();
		assertEquals(ml.size(), 0);
		m.addMission(fiveHoursMission);
		assertEquals(m.getMissionList().size(), 1);
		m.addMission(fiveHoursMission);
		assertEquals(m.getMissionList().size(), 1);
		m.addMission(twoHoursMission);
		assertEquals(m.getMissionList().size(), 2);
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#getMissionForSergeant(spl.army.Sergeant)}.
	 */
	@Test
	public void testGetMissionForSergeant() {
		Sergeant s = new Sergeant("Moshe", "5", "sniper", "10", "shortestMission", "5");
		assertEquals(
				this.missionHolder.getMissionForSergeant(s),
				this.twoHoursMission
				);	
		s = new Sergeant("Moshe", "5", "sniper", "10", "longestMission", "5");
		assertEquals(
				this.missionHolder.getMissionForSergeant(s),
				this.fiveHoursMission
				);		
		s = new Sergeant("Moshe", "5", "sniper", "10", "minItems", "5");
		assertEquals(
				this.missionHolder.getMissionForSergeant(s),
				this.noItemsReqMission
				);		
		s = new Sergeant("Moshe", "5", "sniper", "10", "maxItems", "5");
		assertEquals(
				this.missionHolder.getMissionForSergeant(s),
				this.threeItemsReqMission
				);				
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#getReadyMissionByPriority(java.lang.String)}.
	 */
	@Test
	public void testGetReadyMissionByPriority() {
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("shortestMission",this.dummySergeant),
				this.twoHoursMission
				);
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("longestMission",this.dummySergeant),
				this.fiveHoursMission
				);
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("minItems",this.dummySergeant),
				this.noItemsReqMission
				);
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("maxItems",this.dummySergeant),
				this.threeItemsReqMission
				);
		
		this.fiveHoursMission.setActive();
		this.twoHoursMission.setActive();
		
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("shortestMission",this.dummySergeant),
				this.threeItemsReqMission
				);
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("longestMission",this.dummySergeant),
				this.noItemsReqMission
				);
		
		this.threeItemsReqMission.setActive();
		this.noItemsReqMission.setActive();
		this.twoHoursMission.disableActive();
		this.fiveHoursMission.disableActive();
		
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("minItems",this.dummySergeant),
				this.fiveHoursMission
				);
		assertEquals(
				this.missionHolder.getReadyMissionByPriority("maxItems",this.dummySergeant),
				this.twoHoursMission
				);		
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#count()}.
	 */
	@Test
	public void testCount() {
		assertEquals(this.missionHolder.count(), 4);
	}

	/**
	 * Test method for {@link spl.army.MissionHolder#resetMissionHolder()}.
	 */
	@Test
	public void testResetMissionHolder() {
		this.missionHolder.resetMissionHolder();
		assertEquals(this.missionHolder.count(), 0);
	}

}
